﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PW_kalendarz
{
    class Solver
    {

        private LinkedList<CustomEvent> eventsList = new LinkedList<CustomEvent>();

        public Solver()
        {

        }

        public void addEvent(CustomEvent customEvent) {

            eventsList.AddLast(customEvent);

        }

        public CustomEventWithRate solve()
        {

            LinkedList<CustomEventWithRate> dailyBests = new LinkedList<CustomEventWithRate>();

            foreach(CustomEvent c in eventsList) {

                LinkedList<CustomEvent> friends = new LinkedList<CustomEvent>();

                friends.AddLast(c);

                foreach (CustomEvent e in eventsList)
                {

                    if (c.UserLogin != e.UserLogin && c.Date == e.Date)
                    {

                        friends.AddLast(e);

                    }

                }

                dailyBests.AddLast(searchDailyBest(friends));

            }

            CustomEventWithRate best = new CustomEventWithRate("RESULT", "NOBODY", 0, 0, 0, 0);

            foreach (CustomEventWithRate c in dailyBests)
            {
                if (c.rate > best.rate)
                {
                    best = c;
                }
            }

            return best;
        }

        private CustomEventWithRate searchDailyBest(LinkedList<CustomEvent> list) {

            LinkedList<CustomEventWithRate> dailyBests = new LinkedList<CustomEventWithRate>();

            if (list.Count < 1)
            {
                return null;
            }
            else if (list.Count == 1)
            {
                CustomEventWithRate result = new CustomEventWithRate(list.First.Value);
                return result;
            }
            else
            {
                foreach (CustomEvent c in list)
                {
                    LinkedList<CustomEvent> friends = new LinkedList<CustomEvent>();
                    friends.AddLast(c);

                    foreach (CustomEvent e in list)
                    {
                        if (e != c && e.StartHour >= c.StartHour && e.EndHour <= c.EndHour)
                        {
                            friends.AddLast(e);
                        }
                    }

                    friends.RemoveFirst();

                    int start = c.StartHour;
                    int end = c.EndHour;

                    foreach (CustomEvent x in friends)
                    {
                        start = Math.Max(start, x.StartHour);
                        end = Math.Min(end, x.EndHour);
                    }

                    if (start < end)
                    {
                        dailyBests.AddLast(new CustomEventWithRate("RESULT", "NOBODY", c.Date, start, end, friends.Count + 1));
                    }

                }

                if (dailyBests.Count < 1)
                {

                    return null;

                }
                else
                {
                    CustomEventWithRate best = new CustomEventWithRate("RESULT", "NOBODY", 0, 0, 0, 0);
                    foreach (CustomEventWithRate c in dailyBests)
                    {
                        if (c.rate > best.rate)
                        {
                            best = c;
                        }
                    }
                    return best;
                }

            }

        }

    }
}
